// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var g = require('global')

cc.Class({
    extends: cc.Component,

    properties: {
        prefab: cc.Prefab,
        container: cc.Layout
    },

    start() {

    },

    onLoad() {
        this.data = new g.BlockItem().init(1, 10, 3, 3, "123456789X")

        var padding = this.container.padding = this.container.spacingX * 2

        var w = (this.container.node.width -
            this.data.col * this.container.spacingX - padding * 2
        ) / this.data.col;

        this.container.spacingY = this.container.spacingX

        this.container.cellSize = new cc.Size(w, w)
        var height = (w + this.container.spacingY) * this.data.row + padding * 2
        this.container.node.height = height

        for (var i = 0; i < this.data.values.length; i++) {
            var item = cc.instantiate(this.prefab)
            var data = this.data.values[i]

            item.getComponent('BlockItemTemplate').init({
                id: i,
                filledValue: data
            })
            this.container.node.addChild(item)
            // item.on(cc.Node.EventType.MOUSE_UP, this.onTouchBlock, this)
            item.on(cc.Node.EventType.TOUCH_END, this.onTouchBlock, this)
        }
    },
    onTouchBlock: function (event) {
        var ev = new cc.Event.EventCustom("onFillBlockClick", true)
        var value = event.target.getComponent('BlockItemTemplate').valueStr
        ev.setUserData({ value: value })
        this.node.dispatchEvent(ev)
    }
});
